package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgFixedAssetDataService;
import com.csun.dsms.service.HgFixedAssetInstructionDataService;
import com.csun.dsms.service.HgInvestmentDataService;
import com.csun.dsms.service.HgInvestmentPartDataService;
import com.csun.dsms.utils.PublicUtil;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 招商吧
 */
@RestController
@RequestMapping("/hgInvestment")
public class HgInvestmentController {


    @Resource
    private HgInvestmentDataService hgInvestmentDataService;
    
    @Resource
    private HgInvestmentPartDataService hgInvestmentPartDataService;

    /*
    * */
    @Authorization
    @RequestMapping(value = "/dataAdd",method = RequestMethod.POST)
    public Wrapper dataAdd (@RequestBody HgInvestmentData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgInvestmentDataService.save(hgData);

        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 数据查询
     * */
    @Authorization
    @RequestMapping(value = "/dataListQuery",method = RequestMethod.POST)
    public Wrapper dataListQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgInvestmentData> query = hgInvestmentDataService.queryDataList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导入招商表格数据
     */
    @Authorization
    @RequestMapping(value = "/importInvestmentTableData",method = RequestMethod.POST)
    public Wrapper importInvestmentTableData (@RequestParam("file") MultipartFile file) {
        return hgInvestmentDataService.importInvestmentTableData(file);
    }

    @Authorization
    @RequestMapping(value = "/dataUpdate",method = RequestMethod.POST)
    public Wrapper dataUpdate (@RequestBody HgInvestmentData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgInvestmentDataService.update(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }



    /*
     * */
    @Authorization
    @RequestMapping(value = "/partAdd",method = RequestMethod.POST)
    public Wrapper partAdd (@RequestBody HgInvestmentPartData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgInvestmentPartDataService.save(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 数据查询
     * */
    @Authorization
    @RequestMapping(value = "/partListQuery",method = RequestMethod.POST)
    public Wrapper partListQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgInvestmentPartData> query = hgInvestmentPartDataService.queryDataList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导入招商专班表格数据
     */
    @Authorization
    @RequestMapping(value = "/importInvestmentPartTableData",method = RequestMethod.POST)
    public Wrapper importInvestmentPartTableData (@RequestParam("file") MultipartFile file) {
        return hgInvestmentPartDataService.importInvestmentPartTableData(file);
    }

    @Authorization
    @RequestMapping(value = "/partUpdate",method = RequestMethod.POST)
    public Wrapper partUpdate (@RequestBody HgInvestmentPartData hgData){
        if(PublicUtil.isEmpty(hgData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgInvestmentPartDataService.update(hgData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    @Authorization
    @RequestMapping(value = "/groupByMonth",method = RequestMethod.POST)
    public Wrapper groupByMonth (@RequestBody HgQueryDto hgQueryDto){
        List<HgYearMonthVo> query = hgInvestmentDataService.groupByMonth(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导出招商表格数据模板
     */
    @GetMapping("/downInvestmentTableTemplate")
    public void downInvestmentTableTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "招商表格导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("年度目标数"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("完成个数"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("完成比例"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("同比增速"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("年度目标金额"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("已完成金额"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("完成金额比例"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("投资同比增速"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("年度投资项目数量"));
        sheet.setColumnWidth(12, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(13);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("已完成投资项目数量"));
        sheet.setColumnWidth(13, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(14);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("完工投资项目比率"));
        sheet.setColumnWidth(14, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(15);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("投资项目同比增长"));
        sheet.setColumnWidth(15, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(16);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("年度存储项目金额"));
        sheet.setColumnWidth(16, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(17);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("已完成存储项目金额"));
        sheet.setColumnWidth(17, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(18);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("已完成存储项目金额比率"));
        sheet.setColumnWidth(18, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(19);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("存储项目同比增长"));
        sheet.setColumnWidth(19, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(20);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(20, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(21);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(21, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(22);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(22, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(23);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(23, 20 * 256);

        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出招商专班表格数据模板
     */
    @GetMapping("/downInvestmentPartTableTemplate")
    public void downInvestmentPartTableTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "招商专班表格数据导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("专班名称"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("外出招商"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("接待客商"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("在谈项目"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("签约项目"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("协议投资额"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(13);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(11, 20 * 256);

        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
